%% Aim
% Mark invalid eye data

%% Setup
tic;
clear;
clc;
root_path = pwd; 
addpath(genpath(root_path));

nrole=2;

%% Load data
sub_data=readtable([root_path,'\data\subject\subject.csv']);
nsub=height(sub_data);
beh_data=readtable([root_path,'\data\behavior\behavior.csv']);
eye_data=readtable([root_path,'\data\eye\eye.csv']);

%% basic columns
Subject = beh_data.Subject;
Block = beh_data.Block;
Role = beh_data.Role;
PricePosition = beh_data.PricePosition;
Price = beh_data.Price;
Lottery = beh_data.Lottery;
Choice = beh_data.Choice;
RT = beh_data.RT;

ValidEyePre=NaN(height(beh_data),1);
ValidEyeOption=NaN(height(beh_data),1);

%% Calculate the portion of qualified data points for each trial

% Mark unqualified data points
eye_data.Quality(:)=1;
eye_data.Quality(eye_data.GazeX<=0 | eye_data.GazeY<=0 | isnan(eye_data.GazeX) | isnan(eye_data.GazeY),:)=0;
eye_data.Quality(eye_data.PupilLeft<=0 | isnan(eye_data.PupilLeft),:)=0;

for isub = 1:nsub
    for irole = 1:nrole
        
        edata = eye_data(eye_data.Subject == isub & eye_data.Role == irole,:);
        
        diffs = diff(edata.StimulusName);
        events = find(diffs ~= 0) + 1;
        %stims = edata.StimulusName(events);
        % loop through each of the options         
        for i=1:(length(events)-1)
            include=1;
            if edata.StimulusName(events(i))<=100
               continue;
            end
            option_on=edata.TimeSignal(events(i));
            option_off=edata.TimeSignal(events(i+1));
            pre_time=option_on - 500;
            post_time=option_off + 1500;
            option_quality=edata(edata.TimeSignal >= option_on & edata.TimeSignal < option_off,:).Quality;            
            pre_option_quality=edata(edata.TimeSignal >= pre_time & edata.TimeSignal < option_on,:).Quality;
            ed_option_quality=edata(edata.TimeSignal >= pre_time & edata.TimeSignal <= post_time,:).Quality;
            
            option_valid=mean(option_quality);
            pre_option_valid=mean(pre_option_quality);
            ed_option_valid=mean(ed_option_quality);
            
            price = floor(edata.StimulusName(events(i))/1000);
            lottery = mod(edata.StimulusName(events(i)),1000);
            idx = Subject == isub & ...
                  Role == irole & ...
                  Price == price & ...
                  Lottery == lottery;
            ValidEyeOption(idx,1) = option_valid;              
            ValidEyePre(idx,1) = pre_option_valid;
            ValidEyeEd(idx,1) = ed_option_valid;
            
        end
    end
end

%% Mark validity of each trials
ValidGaze=ValidEyeOption>0.5;
ValidPupil=ValidEyePre>0.5 & ValidEyeEd>0.5;

%% Generate a table .csv for all subjects

eye_trial_data = table(Subject, Block, Role, PricePosition, Price, Lottery, Choice, RT, ...
                        ValidEyeOption, ValidEyePre, ValidEyeEd, ValidGaze, ValidPupil,...
                        'VariableNames',...
                        {'Subject', 'Block', 'Role', 'PricePosition', 'Price', 'Lottery', 'Choice', 'RT',...
                         'ValidEyeOption','ValidEyePre','ValidEyeEd','ValidGaze','ValidPupil'});
writetable(eye_trial_data, [root_path, '\data\eye\eye_trial.csv']);
toc;

%%

height(eye_trial_data(eye_trial_data.ValidGaze==0,:)) / height(eye_trial_data);
height(eye_trial_data(eye_trial_data.ValidPupil==0,:)) / height(eye_trial_data);
